<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="onChangeReset">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" class="w-[260px]">
          <a-input v-model:value="formState.zjm" placeholder="配血结果" />
        </a-form-item>
        <a-form-item name="isPass" class="w-[280px] ml-2">
          <a-radio-group v-model:value="formState.isPass">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">合格</a-radio>
            <a-radio value="0">不合格</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <a-button type="primary" @click="editOpen = true">新增</a-button>
      </a-space>
      <BaseTable :loading="loading" column-code="000642" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'isPass'">
            <a-switch v-model:checked="record.isPass" @change="onChangeSwitch(record)" />
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formState = ref({
  isPass: ''
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/BldTransfusionRecord/getBldTransfusionResultList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      isPass: formState.value?.isPass
    }
  },
  formatResult: (result) => {
    result.forEach((item) => {
      if (item.isPass === '1') {
        item.isPass = true
      } else {
        item.isPass = false
      }
    })
    return result
  }
})

const onChangeSwitch = async (row) => {
  const { code, msg } = await post('/bloodm/BldTransfusionRecord/doSaveBldTransfusionResult', { id: row.id, isPass: row?.isPass ? 1 : 0 })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

const onChangeReset = () => {
  formState.value = {}
  formState.value.isPass = ''
  getData()
}
</script>
